package org.dromara.business.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.User;
import org.dromara.business.domain.UserFlowing;
import org.dromara.business.domain.bo.AdvertisingBo;
import org.dromara.business.mapper.UserFlowingMapper;
import org.dromara.business.mapper.UserMapper;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/home")
public class HomePageController {
    private final UserMapper baseMapper;
    private final UserFlowingMapper userFlowingMapper;

    @GetMapping()

    public R<Map<String, Object>> index() {
        Map<String, Object> map = new HashMap<>();

        // 查询数据库中的所有用户数量，并将结果存储在map中
        getUserNumer(map);

        // 计算今日新增用户数量及按VIP等级统计的用户数量
        getUserFlowing(map);

        return R.ok(map);
    }
    /**
     *
     *
     * @param map
     */
    private void getUserFlowing(Map<String, Object> map) {
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 使用LambdaQueryWrapper构建查询条件，查询类型为"2"的用户流水记录
        LambdaQueryWrapper<UserFlowing> lqwUserFlowing = Wrappers.lambdaQuery();
        lqwUserFlowing.eq(UserFlowing::getType, "2");
        List<UserFlowing> userFlowings = userFlowingMapper.selectList(lqwUserFlowing);
        BigDecimal allwithdrawal=new BigDecimal("0.0");
        BigDecimal withdrawal=new BigDecimal("0.0");

        if (ObjectUtil.isNotNull(userFlowings)){
            // 计算总提现及今日提现金额
             allwithdrawal = userFlowings.stream().map(UserFlowing::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add); // 总提现

             withdrawal = userFlowings.stream().filter(o -> {
                LocalDate userCreationDate = o.getCreateTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
                return userCreationDate.equals(today);
            }).map(UserFlowing::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add); // 今日提现
        }

        map.put("allWithdrawal", allwithdrawal.multiply(new BigDecimal("-1"))); // 提现(元)

        map.put("withdrawal", withdrawal.multiply(new BigDecimal("-1"))); // 提现(元)

    }


    /**
     *
     * @param map
     */
    private void getUserNumer(Map<String, Object> map) {

        // 获取当前日期
        LocalDate today = LocalDate.now();
        List<User> users = baseMapper.selectList();
        int allAddUser =0;
        long allUser =0;
        long allWeekvip =0;
        long allMonthvip =0;
        long allYearVip =0;
        long adduser =0;
        long user =0;
        long weekvip =0;
        long monthvip =0;
        long uearVip =0;
        if (ObjectUtil.isNotNull(users)){
            // 计算总用户数量
            allAddUser = users.size();
            // 根据用户VIP等级统计用户数量
            allUser = users.stream().filter(o -> "0".equals(o.getVip())).count(); // 普通用户
             allWeekvip = users.stream().filter(o -> "1".equals(o.getVip())).count(); // 周会员
             allMonthvip = users.stream().filter(o -> "2".equals(o.getVip())).count(); // 月会员
             allYearVip = users.stream().filter(o -> "3".equals(o.getVip())).count(); // 年会员
            // 使用Stream API过滤出创建时间是今天的用户
            List<User> todayUsers = users.stream()
                .filter(o -> {
                    LocalDate userCreationDate = o.getCreateTime().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();
                    return userCreationDate.equals(today);
                })
                .collect(Collectors.toList());

            if (ObjectUtil.isNotNull(todayUsers)){
                 adduser = todayUsers.size();
                 user = todayUsers.stream().filter(o -> "0".equals(o.getVip())).count(); // 今日普通用户
            }
            // 使用Stream API过滤出创建时间是今天的用户
            List<User> collect = users.stream().filter(o->null!= o.getVipTime())
                .filter(o -> {
                    LocalDate userCreationDate = o.getVipTime().toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();
                    return userCreationDate.equals(today);
                })
                .collect(Collectors.toList());

            if (ObjectUtil.isNotNull(collect)){
                weekvip = collect.stream().filter(o -> "1".equals(o.getVip())).count(); // 今日周会员
                monthvip = collect.stream().filter(o -> "2".equals(o.getVip())).count(); // 今日月会员
                uearVip = collect.stream().filter(o -> "3".equals(o.getVip())).count(); // 今日年会员
            }

        }


        map.put("allAddUser", allAddUser); // 总用户数量
        map.put("allUser", allUser); // 普通用户
        map.put("allWeekVip", allWeekvip); // 周会员
        map.put("allMonthVip", allMonthvip); // 月会员
        map.put("allYearVip", allYearVip); // 年会员
        map.put("addUser", adduser); // 新增用户数量
        map.put("user", user); // 今日普通用户
        map.put("weekVip", weekvip); // 今日周会员
        map.put("monthVip", monthvip); // 今日月会员
        map.put("yearVip", uearVip); // 今日年会员



    }


}
